package com.sdwz.borrow.service.impl;

import com.sdwz.borrow.entity.PenaltyInfo;
import com.sdwz.borrow.mapper.PenaltyInfoMapper;
import com.sdwz.borrow.service.PenaltyInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 罚款信息表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-30
 */
@Service
public class PenaltyInfoServiceImpl extends ServiceImpl<PenaltyInfoMapper, PenaltyInfo> implements PenaltyInfoService {

    /**
     * 根据天数计算罚款
     * @param day
     * @param price
     * @return
     */
    @Override
    public BigDecimal calculateAmount(int day, BigDecimal price){
        BigDecimal money = new BigDecimal(0);
        if (day<=10){
            money = money.add(price.multiply(new BigDecimal("0.05")).multiply(new BigDecimal(day)));
        }
        if (day >10 && day<=15){
            money = money.multiply(new BigDecimal("0.05")).multiply(new BigDecimal(5)).add(money.multiply(new BigDecimal("0.1")).multiply(new BigDecimal(day-5)));
        }
        if (day>15){
            money = price.multiply(new BigDecimal(2));
        }
        return money;
    }

    /**
     * 获取超期天数
     * @param startDay
     * @param endDay
     * @return
     */
    @Override
    public int calculateDay(Date startDay, Date endDay){
        int i=0;

        Date nextDay=startDay;

        while(nextDay.before(endDay)){//当明天不在结束时间之前是终止循环
            Calendar cld = Calendar.getInstance();
            cld.setTime(startDay);
            cld.add(Calendar.DATE, 1);
            startDay = cld.getTime();
            //获得下一天日期字符串
            nextDay = startDay;
            i++;
        }
        return i;
    }
}
